package yunquan.wang.base.dao;

import java.util.Collection;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;

@SuppressWarnings("rawtypes")
public class QueryHelper {
	private static final String OR_SEPARATOR = "_OR_";
	public static Specification filter(final String name,
			final MatchType matchType, final Object value) {
		switch (matchType) {
		case EQ:
		return DaoUtils.eq(name, value);
		case LIKE:
			return DaoUtils.like(name, value);
		case GT:
			return DaoUtils.gt(name, value);
		case LT:
			return DaoUtils.lt(name, value);
		case GE:
			return DaoUtils.ge(name, value);
		case LE:
			return DaoUtils.le(name, value);
		case NI:
			return DaoUtils.notin(name, value);
		case NE:
			return DaoUtils.ne(name, value);
		case SQL:
			break;
		case EX:
			//return DaoUtils.exist (name, value);
		case IN:
			return DaoUtils.in(name, value);
		}
		return null;
	}

 public static Specification addSpecification(Specification sp,String propertyName,MatchType mt, String propertyValue)
 {
	 Specification filter=filter(propertyName,mt,propertyValue);
	 if(sp==null)
	 {
		 sp=Specifications.where(filter);
	 }
	 else
	 {
		 sp=	 Specifications.where(sp).and(filter);
	 }
	 
	 return sp;
 }

	public static Specification listFilter(	final Collection<PropertyFilter> filters) {
		Specifications specification = null;
		for (PropertyFilter filter : filters) {
			String proName = filter.getName();
			if (!proName.contains(OR_SEPARATOR)) {
				Specification criterion = filter(proName,
						filter.getMatchType(), filter.getValue());
				if (specification == null) {
					specification = Specifications.where(criterion);
				} else {
					specification = Specifications.where(specification).and(
							criterion);
				}
			} else {
				for (String param :StringUtils.splitByWholeSeparator(filter.getName(),OR_SEPARATOR)) {
					Specification criterion = filter(param,
							filter.getMatchType(), filter.getValue());
					if (specification == null) {
						specification = Specifications.where(criterion);
					} else {
						specification = Specifications.where(specification).or(
								criterion);
					}
				}

			}
		}
		return specification;
	}

 

	/**
	 * 抓取子关联属性
	 * 
	 * @param porpertyName
	 * @return
	 */
	public static Specification fetchMany(final String porpertyName) {
		return new Specification() {
			public Predicate toPredicate(Root root, CriteriaQuery query,
					CriteriaBuilder cb) {
				root.fetch(porpertyName);
				return null;
			}
		};
	}

	/**
	 * 抓取父关联属性 TODO 没有实现
	 * 
	 * @param porpertyName
	 * @return
	 */
	public static Specification fetchOne(final String porpertyName) {
		return new Specification() {
			public Predicate toPredicate(Root root, CriteriaQuery query,
					CriteriaBuilder cb) {
				return null;
			}
		};
	}

	public Specification byExample(Object obj) {
		return new Specification() {

			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query,
					CriteriaBuilder cb) {
				// TODO BY example的查询

				return null;
			}
		};
	}

}
